﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace Miao.SourceGen.Learn
{
    public static class MiaoSyntaxFinder
    {
        public static bool IsSyntaxTargetForGeneration(SyntaxNode syntaxNode, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            //Debugger.Launch();

            // IdentifierNameSyntax//检查类是否实现了一个接口


            if (syntaxNode is ClassDeclarationSyntax structDeclarationSyntax)//整个一行类咏唱
            {
                if (structDeclarationSyntax.BaseList == null)
                    return false;
                foreach (var baseType in structDeclarationSyntax.BaseList.Types)
                {
                    if (baseType.Type is IdentifierNameSyntax { Identifier: { ValueText: "IUIExtend" } })
                    {
                        return true;
                    }

                }

            }

            return false;

        }

        public static ClassDeclarationSyntax GetSemanticTargetForGeneration(GeneratorSyntaxContext ctx, CancellationToken cancellationToken)
        {
            var classSyntax = (ClassDeclarationSyntax)ctx.Node;
            //Debugger.Launch();

            foreach (var baseTypeSyntax in classSyntax.BaseList!.Types)//!.表示BaseList本身不会为空。
            {
                if (ctx.SemanticModel.GetTypeInfo(baseTypeSyntax.Type).Type.Name == "global::Unity.Entities.IJobEntity")
                {
                    return classSyntax;
                }

                return classSyntax;//先这样返回

               

            }
            return null;

        }

    }
}
